from django.shortcuts import render
from django.http import HttpResponse
from photomanageapp.models import Photos
from datetime import datetime
import time
import os
from PIL import Image
from django.core.paginator import Paginator

# Create your views here.
def index(request):
    return render(request,"photomanageapp/index.html")

# 浏览相册列表
def photoList(request):
    pindex=request.GET.get("pg")# 页码
    # 获取相册列表
    photodata=Photos.objects.all()
    pagelist=[]
    # # 获取分页信息
    p = Paginator(photodata,2)
    if pindex is None or pindex == '':
        pindex = 1
    pindex = int(pindex)
    photodata2 = p.page(pindex)
    pagelist = p.page_range
    context={"photodata":photodata2,"pagelist":pagelist}
    return render(request,"photomanageapp/photo/index.html",context)

# 添加相册
def addPhoto(request):
    return render(request, "photomanageapp/photo/add.html")

# 上传图片
def uploadPics(photofile):
    filename = str(time.time()) + "." + photofile.name.split(".").pop()
    destination = open("./static/uploadpics/" + filename, "wb+")
    for chunk in photofile.chunks():
        destination.write(chunk)
    destination.close()
    # 图片缩放
    img_thumb=Image.open("./static/uploadpics/"+filename)
    img_thumb.thumbnail((70,70))
    img_thumb.save("./static/uploadpics/s_"+filename,None)
    return filename

# 提交保存相册
def insertPhoto(request):
    # 获取相册信息
    try:
        photo=Photos()
        photo.title=request.POST["title"]
        photofile = request.FILES.get("photourl","")
        if not photofile:
            context = {"message": "没有上传文件信息！"}
        else:
            filename=uploadPics(photofile)# 上传图片
            photo.updateTime=datetime.now()
            photo.photoUrl=filename
            photo.save()
            print(photo.photoUrl)
            context={"message":"添加成功！"}
    except Exception as err:
        context = {"message": "添加失败！"+err}
    return render(request,"photomanageapp/photo/info.html",context)

# 删除相册
def delPhoto(request,pid):
    # return HttpResponse(pid)
    try:
        obj=Photos.objects.get(id=pid)
        # 删除图片
        picname=obj.photoUrl
        os.remove("./static/uploadpics/"+picname)
        os.remove("./static/uploadpics/s_" + picname)
        obj.delete()
        context = {"message": "删除成功！"}
    except Exception as err:
        context = {"message": "删除失败！"}
    return render(request, "photomanageapp/photo/info.html", context)

# 编辑相册信息
def editPhoto(request,pid):
    try:
        obj=Photos.objects.get(id=pid)
        context = {"photo": obj}
        return render(request, "photomanageapp/photo/edit.html", context)
    except Exception as err:
        print(err)
        context = {"message": "没有找到要修改的信息！"}
        return render(request, "photomanageapp/photo/info.html", context)

# 保存相册编辑信息
def updatePhoto(request):
    try:
        photo=Photos.objects.get(id=request.POST["id"])
        photo.title=request.POST["title"]
        photofile = request.FILES.get("photourl", "")
        photourl=request.POST["photourl_old"]
        if not photofile:
            # 如果没有上传新图片，是否有原图片地址
            photourl = request.POST["photourl_old"]
        else:
            photourl=uploadPics(photofile)  # 上传图片
        if photourl=="":
            context = {"message": "没有上传文件信息！"}
        else:
            photo.photoUrl = photourl
            photo.updateTime=datetime.now()
            photo.save()
            context={"message":"修改成功！"}
    except Exception as err:
        context = {"message": "修改失败！"+err}
    return render(request,"photomanageapp/photo/info.html",context)